﻿<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:primitives="clr-namespace:BoilenEditor.Primitives"
        x:Class="BoilenEditor.SnippetWindow"
        Title="Insert Snippet"
        MinWidth="585"
        SizeToContent="WidthAndHeight"
        ResizeMode="NoResize"
        ShowInTaskbar="False">
    <Window.CommandBindings>
        <CommandBinding Command="Select"
                        Executed="SelectExecuted"
                        CanExecute="SelectCanExecute" />
        <CommandBinding Command="Close"
                        Executed="CancelExecuted" />
    </Window.CommandBindings>
    <Window.InputBindings>
        <KeyBinding Command="Close"
                    Gesture="Ctrl+W" />
    </Window.InputBindings>

    <DockPanel Margin="5">
        <StackPanel DockPanel.Dock="Bottom"
                    Orientation="Horizontal"
                    HorizontalAlignment="Right">
            <Button Style="{StaticResource DialogButton}"
                    IsDefault="True"
                    Content="_Insert"
                    Command="Select" />
            <Button Style="{StaticResource DialogButton}"
                    IsCancel="True"
                    Content="_Cancel" />
        </StackPanel>

        <Rectangle DockPanel.Dock="Bottom"
                   Height="3"
                   HorizontalAlignment="Stretch"
                   Margin="5"
                   Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />

        <ComboBox Name="declarationsSelector_"
                  DockPanel.Dock="Top"
                  Margin="5"
                  ItemsSource="{Binding Declarations}"
                  SelectedIndex="{Binding SelectedDeclarationIndex}">
            <ComboBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Grid.IsSharedSizeScope="True" />
                </ItemsPanelTemplate>
            </ComboBox.ItemsPanel>
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <Grid TextSearch.Text="{Binding DisplayName, Mode=OneTime}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"
                                              SharedSizeGroup="Category" />
                            <ColumnDefinition Width="Auto"
                                              SharedSizeGroup="Name" />
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="0"
                                   HorizontalAlignment="Right"
                                   Foreground="{StaticResource SecondaryInfoBrush}"
                                   Text="{Binding Category, StringFormat='[{0}] ', Mode=OneTime}" />
                        <TextBlock Grid.Column="1"
                                   Text="{Binding DisplayName, Mode=OneTime}"
                                   HorizontalAlignment="Left" />
                    </Grid>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

        <GroupBox DockPanel.Dock="Top"
                  Header="_Arguments (required)"
                  Margin="5,0,5,5"
                  Visibility="{Binding Path=SelectedItem.Arguments.Count, ElementName=declarationsSelector_, Converter={StaticResource CountToVisibility}}">
            <ItemsControl x:Name="argumentsList_"
                          Grid.IsSharedSizeScope="True"
                          Margin="5,0,5,5"
                          IsTabStop="False"
                          HorizontalAlignment="Stretch"
                          ItemsSource="{Binding SelectedItem.Arguments, ElementName=declarationsSelector_}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"
                                                  MinWidth="{StaticResource MinimumWidth}"
                                                  SharedSizeGroup="Name" />
                                <ColumnDefinition Width="10" />
                                <ColumnDefinition Width="*"
                                                  MinWidth="{StaticResource MinimumWidth}"
                                                  SharedSizeGroup="Value" />
                                <ColumnDefinition Width="10" />
                                <ColumnDefinition Width="Auto"
                                                  SharedSizeGroup="Type" />
                                <ColumnDefinition Width="10" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>

                            <Label Grid.Column="0"
                                   Content="{Binding Name, Mode=OneWay}"
                                   HorizontalAlignment="Right" />
                            <TextBox Grid.Column="2"
                                     Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                     HorizontalAlignment="Stretch" />
                            <TextBlock Grid.Column="4"
                                       Text="{Binding TypeName, Mode=OneWay}"
                                       Foreground="{StaticResource SecondaryInfoBrush}"
                                       HorizontalAlignment="Left"
                                       VerticalAlignment="Center" />
                            <TextBlock Grid.Column="6"
                                       Text="{Binding Format, Mode=OneTime}"
                                       Foreground="{StaticResource SecondaryInfoBrush}"
                                       HorizontalAlignment="Left"
                                       VerticalAlignment="Center" />
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </GroupBox>

        <GroupBox Header="C_onfiguration (optional)"
                  Margin="5,0,5,5"
                  Visibility="{Binding Path=SelectedItem.Members.Count, ElementName=declarationsSelector_, Converter={StaticResource CountToVisibility}}">
            <ItemsControl Grid.IsSharedSizeScope="True"
                          ItemsSource="{Binding SelectedItem.Members, ElementName=declarationsSelector_}"
                          Margin="5"
                          IsTabStop="False">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"
                                                  MinWidth="{StaticResource MinimumWidth}"
                                                  SharedSizeGroup="Name" />
                                <ColumnDefinition Width="10" />
                                <ColumnDefinition Width="*"
                                                  MinWidth="{StaticResource MinimumWidth}"
                                                  SharedSizeGroup="Value" />
                                <ColumnDefinition Width="10" />
                                <ColumnDefinition Width="Auto"
                                                  SharedSizeGroup="Type" />
                            </Grid.ColumnDefinitions>

                            <Label Grid.Column="0"
                                   Content="{Binding SharedName, Mode=OneWay}"
                                   HorizontalAlignment="Right"
                                   Visibility="{Binding Path=SharedName, Converter={StaticResource StringToVisibility}}" />
                            <ComboBox Grid.Column="0"
                                      ItemsSource="{Binding Members, Mode=OneWay}"
                                      SelectedIndex="{Binding SelectedMemberIndex, Mode=TwoWay}"
                                      VerticalContentAlignment="Center"
                                      Visibility="{Binding Path=SharedName, Converter={StaticResource NoStringToVisibility}}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Name}" />
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ComboBox>

                            <TextBox Grid.Column="2"
                                     Text="{Binding SharedValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                     Visibility="{Binding Path=SelectedMember.Entries.Count, Converter={StaticResource SingleToVisibility}}" />
                            <ComboBox Grid.Column="2"
                                      ItemsSource="{Binding SelectedMember.Entries}"
                                      IsEditable="{Binding IsEditable}"
                                      Text="{Binding SharedValue, Mode=TwoWay}"
                                      Visibility="{Binding Path=SelectedMember.Entries.Count, Converter={StaticResource MultipleToVisibility}}" />

                            <TextBlock Grid.Column="4"
                                       Text="{Binding MergedTypeName, Mode=OneWay}"
                                       Foreground="{StaticResource SecondaryInfoBrush}"
                                       HorizontalAlignment="Left"
                                       VerticalAlignment="Center"
                                       Visibility="{Binding Path=SharedName, Converter={StaticResource StringToVisibility}}" />
                            <TextBlock Grid.Column="4"
                                       Text="{Binding Path=SelectedMember.TypeName, Mode=OneWay}"
                                       Foreground="{StaticResource SecondaryInfoBrush}"
                                       HorizontalAlignment="Left"
                                       VerticalAlignment="Center"
                                       Visibility="{Binding Path=SharedName, Converter={StaticResource NoStringToVisibility}}" />
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </GroupBox>
    </DockPanel>

    <FocusManager.FocusedElement>
        <Binding ElementName="declarationsSelector_"
                 Mode="OneTime" />
    </FocusManager.FocusedElement>
</Window>
